package com.xinghou.platform.dao.mange.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.mange.MessageDAO;
import com.xinghou.platform.model.main.Message;


/**
 * 
 * @author wlw
 *
 */
@Repository
public class MessageDAOImpl implements MessageDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public List<Message> getMsg(Short status) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT m.Msg_id,m.Sender_id,m.Receiver_id,m.Msg_type,m.Origin,m.TEXT,m.Attach_id,m.Jump_url,m.Jump_native_page,m.STATUS,m.Create_time FROM message m,user u WHERE" 
				+" m.Status=? AND m.Receiver_id=u.User_id AND u.Password IS NULL ORDER BY m.Create_time";
		List<Message> messagelist = jdbcTemplate.query(sql, new Object[] {status}, new MessageMapper());
		CustomerContextHolder.clearCustomerType();
		return messagelist;
	}
	
	private static final class MessageMapper implements RowMapper<Message> {
		public Message mapRow(ResultSet rs, int rowNum) throws SQLException {
			Message message = new Message();
			message.setMsgId(rs.getInt("Msg_id"));
			message.setSenderId(rs.getInt("Sender_id"));
			message.setReceiverId(rs.getInt("Receiver_id"));
			message.setMsgType(rs.getShort("Msg_type"));
			message.setOrigin(rs.getShort("Origin"));
			message.setText(rs.getString("Text"));
			message.setAttachId(rs.getInt("Attach_id"));
			message.setJumpUrl(rs.getString("Jump_url"));
			message.setJumpNativePage(rs.getString("Jump_native_page"));
			message.setStatus(rs.getShort("Status"));
			//message.setCreateTime(rs.getTimestamp("Create_time"));
			return message;
		}
	}

	public int updateMessage(Message message) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"UPDATE message SET Sender_id=?,Receiver_id=?,Msg_type=?,Origin=?,TEXT=?,Attach_id=?,Jump_url=?,Jump_native_page=?,STATUS=?,Create_time=? WHERE Msg_id=?"	
				);
		Object[] params = {message.getSenderId(),message.getReceiverId(),message.getMsgType(),message.getOrigin(),message.getText(),message.getAttachId(),
				message.getJumpUrl(),message.getJumpNativePage(),message.getStatus(),message.getCreateTime(),message.getMsgId()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	public int saveMsg(Message message) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"INSERT INTO message ( Sender_id, Receiver_id, Msg_type, Origin, TEXT, Attach_id, Jump_url, Jump_native_page, STATUS, Create_time) VALUES (?,?,?,?,?,?,?,?,?,now());"
				);
		Object[] params = {message.getSenderId(),message.getReceiverId(),message.getMsgType(),message.getOrigin(),message.getText(),message.getAttachId(),
				message.getJumpUrl(),message.getJumpNativePage(),message.getStatus()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}



	public int updateMessageBymsgid(Integer msgId) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"UPDATE message SET STATUS=1 WHERE Msg_id=?"	
				);
		Object[] params = {msgId};	
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}


	@Override
	public List<Message> getMsgss(int trueid, int fakeid, Short status)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		StringBuffer sb = new StringBuffer();
		Object[] obj = new Object[]{};
		sb.append("SELECT Msg_id,Sender_id,Receiver_id,Msg_type,Origin,TEXT,Attach_id,Jump_url,Jump_native_page,STATUS,Create_time FROM message WHERE" 
				+" Sender_id=? AND Receiver_id=? or Sender_id=? AND Receiver_id=?");

		obj = new Object[]{trueid,fakeid,fakeid,trueid};
		sb.append( "  ORDER BY Create_time");
		List<Message> messagelist = jdbcTemplate.query(sb.toString(), obj, new MessageMapper());
		CustomerContextHolder.clearCustomerType();
		return messagelist;
	}

	@Override
	public List<Message> getMsggsslist(Integer msgId)throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
	     String sql="SELECT Msg_id,Sender_id,Receiver_id,Msg_type,Origin,TEXT,Attach_id,Jump_url,Jump_native_page,STATUS,Create_time FROM  message m  WHERE m.Msg_id=?";
		List<Message> messagelist = jdbcTemplate.query(sql, new Object[] {msgId}, new MessageMapper());
		CustomerContextHolder.clearCustomerType();
		return messagelist;
	
	}

	@Override
	public List<Message> getMsgss(int trueid, int fakeid) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		StringBuffer sb = new StringBuffer();
		Object[] obj = new Object[]{};
		sb.append("SELECT Msg_id,Sender_id,Receiver_id,Msg_type,Origin,TEXT,Attach_id,Jump_url,Jump_native_page,STATUS,Create_time FROM message WHERE" 
				+" Sender_id=? AND Receiver_id=? AND STATUS=0");
		obj = new Object[]{trueid,fakeid};
		sb.append( "  ORDER BY Create_time");
		List<Message> messagelist = jdbcTemplate.query(sb.toString(), obj, new MessageMapper());
		CustomerContextHolder.clearCustomerType();
		return messagelist;
	}


}
